package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.pojo.DtsCoupon;
import com.qf.pojo.DtsCouponUser;
import com.qf.service.DtsCouponService;
import com.qf.service.DtsCouponUserService;
import com.qf.util.ResponseUtil;
import jdk.net.SocketFlow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.plaf.basic.BasicScrollPaneUI;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/admin/coupon")
@RestController
@CrossOrigin("*")
public class AdminCouponController {

    @Autowired
    DtsCouponService couponService;

    @Autowired
    DtsCouponUserService couponUserService;

    @RequestMapping("/list")
    public Object list(
        @RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
        @RequestParam(name = "limit",required = false,defaultValue = "20") Integer limit,
        String name,
        String type,
        String status,
        @RequestParam(name = "sort",required = false,defaultValue = "add_time") String sort,
        @RequestParam(name = "order",required = false,defaultValue = "desc") String order){
        IPage<DtsCoupon> iPage = couponService.findPage(page,limit,name,type,status,sort,order);

        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/read")
    public Object read(Integer id){
        DtsCoupon coupon=couponService.findAll(id);

        return ResponseUtil.ok(coupon);
    }

    @RequestMapping("/listuser")
    public Object listuser(
            @RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
            @RequestParam(name = "limit",required = false,defaultValue = "20") Integer limit,
            String couponId,
            String userId,
            String status,
            @RequestParam(name = "sort",required = false,defaultValue = "add_time") String sort,
            @RequestParam(name = "order",required = false,defaultValue = "desc") String order){

        IPage<DtsCouponUser> iPage = couponUserService.findPage(page,limit,couponId,userId,status,sort,order);

        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/update")
    public Object update(@RequestBody DtsCoupon coupon){
        couponService.update(coupon);
        return ResponseUtil.ok();
    }

    @RequestMapping("/create")
    public Object add(@RequestBody DtsCoupon coupon){
         DtsCoupon coupon1=couponService.add(coupon);
         return ResponseUtil.ok(coupon1);
    }

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsCoupon coupon){
        return couponService.delete(coupon);

    }
}
